<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class ShareLatticeModel extends AbstractModel
{
    protected $tableName = 't_share_lattice';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws Exception|Throwable
     */
    public function getDynamic($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addDynamic($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('共享格子铺表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('uid', 25)->setDefaultValue(0)->setIsNotNull()->setColumnComment('用户id');
        $table->colInt('topic', 3)->setDefaultValue(0)->setIsNotNull()->setColumnComment('话题id');
        $table->colVarChar('from', 120)->setDefaultValue('')->setIsNotNull()->setColumnComment('来自');
        $table->colVarChar('content', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('内容');
        $table->colDouble('price', 8,2)->setDefaultValue(0.00)->setColumnComment('卖价');
        $table->colInt('contact_type',1)->setDefaultValue(0)->setColumnComment('联系方式：0.不显示，1.微信，2.电话');
        $table->colVarChar('contact',64)->setDefaultValue('')->setColumnComment('联系方式');
        $table->colVarChar('location', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('所在位置');
        $table->colText('images')->setColumnComment('图片');
        $table->colInt('status', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('状态:0.发布，1.已转让');
        $table->colInt('see', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('浏览');
        $table->colInt('like', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('点赞');
        $table->colInt('comment', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('评论');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('cuid_index', 'uid');
        $table->indexNormal('topic_index', 'topic');
        $table->indexNormal('see_index', 'see');
        $table->indexNormal('like_index', 'like');
        $table->indexNormal('comment_index', 'comment');

        return $table;
    }
}